﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace TaskLib
{
    public class TaskFactory
    {
        private readonly List<Task> tasks = new List<Task>();

        public ReadOnlyCollection<Task> Tasks
        {
            get { return this.tasks.AsReadOnly(); }
        }

        private long nextId = 0;

        public Task CreateTask(string description, DateTime scheduled, string comment = null)
        {
            Task task = new Task(nextId, description, scheduled, comment);
            tasks.Add(task);
            nextId++;
            return task;
        }

        public bool MarkCompleted(long id, DateTime completed)
        {
            Task task = GetTask(id);
            if (task != null)
            {
                task.Completed = completed;
                return true;
            }
            return false;
        }

        public bool MarkCompleted(long id)
        {
            return MarkCompleted(id, DateTime.Now);
        }

        private Task GetTask(long id)
        {
            foreach (Task task in tasks)
            {
                if (task.ID == id)
                {
                    return task;
                }
            }   
            return null;
        }
    }

    public class Task
    {
        private readonly long id;
        public long ID  { get { return this.id; } }

        private readonly string description;
        public string Description { get { return description; } }

        private readonly DateTime scheduled;
        public DateTime Scheduled { get { return scheduled; } }

        private DateTime completed;
        public DateTime Completed
		{ 
			get { return completed; } 
			set { completed = value; }
        }

        private string comment;
        public string Comment { get { return comment; } }

        public Task(long id, string description, DateTime scheduled, string comment = null)
        {
            this.id = id;
            this.description = description;
            this.scheduled = scheduled;
            this.comment = comment;

            this.completed = DateTime.MinValue;
        }
    }
}
